from ai_manager import AIManager
from voice_assistant import VoiceAssistant
from text_generation_assistant import TextGenerationAssistant
from image_generation_assistant import ImageGenerationAssistant
from data_analysis_assistant import DataAnalysisAssistant
from translation_assistant import TranslationAssistant

class Main:
    def __init__(self):
        self.ai_manager = AIManager(20)  # 初始化管理器（最大容量20）
    
    def run(self):
        print("欢迎使用 AI助手管理器 v1.0")
        # 循环显示菜单，直到用户选择退出
        while True:
            self.show_menu()
            choice = self.get_valid_choice()
            if choice == 0:
                print("感谢使用，再见！")
                break
            self.handle_choice(choice)
    
    def show_menu(self):
        print("\n========== 🤖AI助手管理器 ==========")
        print("1. 查看所有AI助手列表")
        print("2. 根据ID查询助手详情")
        print("3. 添加新的AI助手")
        print("4. 根据ID修改助手信息")
        print("5. 删除AI助手（按ID）")
        print("6. 启动AI助手服务")
        print("7. 停止AI助手服务")
        print("8. 为AI助手分配任务")
        print("9. 显示系统统计信息")
        print("0. 退出系统")
        print("===================================")
        print("请选择操作: ", end="")
    
    def get_valid_choice(self):
        while True:
            try:
                return int(input().strip())
            except ValueError:
                print("输入无效，请输入0-9之间的数字：", end="")
    
    def handle_choice(self, choice):
        if choice == 1:
            self.show_all_assistants()
        elif choice == 2:
            self.query_assistant_by_id()
        elif choice == 3:
            self.add_new_assistant()
        elif choice == 4:
            self.update_assistant_info()
        elif choice == 5:
            self.delete_assistant_by_id()
        elif choice == 6:
            self.start_assistant_service()
        elif choice == 7:
            self.stop_assistant_service()
        elif choice == 8:
            self.assign_task_to_assistant()
        elif choice == 9:
            self.show_statistics()
        else:
            print("❌ 无效操作，请重新选择！")
    
    def show_all_assistants(self):
        list = self.ai_manager.get_all_assistants()
        if not list:
            print("ℹ️ 当前无AI助手数据")
            return
        print("所有AI助手列表：")
        for i, assistant in enumerate(list, 1):
            print(f"{i}. {assistant}")
    
    def query_assistant_by_id(self):
        print("请输入要查询的AI助手ID: ", end="")
        id = input().strip()
        assistant = self.ai_manager.find_assistant_by_id(id)
        if assistant is None:
            print(f"❌ 未找到ID为{id}的助手")
        else:
            print(f"✅ 查询结果: {assistant}")
    
    def add_new_assistant(self):
        print("添加新AI助手")
        print("请输入ID: ", end="")
        id = input().strip()
        print("请输入名称: ", end="")
        name = input().strip()
        print("请输入版本号: ", end="")
        version = input().strip()
        print("是否在线？(1-是, 0-否): ", end="")
        is_online = input().strip() == "1"
        
        score = 0
        while True:
            try:
                print("请输入能力评分(0-100): ", end="")
                score = int(input().strip())
                if 0 <= score <= 100:
                    break
                else:
                    print("❌ 评分无效（需0-100），请重新输入！")
            except ValueError:
                print("❌ 评分需为数字，请重新输入！")
        
        print("请选择助手类型（1-数据分析 2-图片生成 3-文本生成 4-翻译 5-语音）: ", end="")
        type_choice = self.get_valid_type_choice()
        new_assistant = None
        
        if type_choice == 1:
            print("请输入支持的数据格式（如CSV,Excel）: ", end="")
            data_formats = input().strip()
            new_assistant = DataAnalysisAssistant(id, name, version, is_online, score, data_formats)
        elif type_choice == 2:
            print("请输入最大支持分辨率（如4K,8K）: ", end="")
            resolution = input().strip()
            new_assistant = ImageGenerationAssistant(id, name, version, is_online, score, resolution)
        elif type_choice == 3:
            print("请输入支持的文本类型（如诗歌,文案）: ", end="")
            text_types = input().strip()
            new_assistant = TextGenerationAssistant(id, name, version, is_online, score, text_types)
        elif type_choice == 4:
            print("请输入支持的翻译语言对（如中英,中日）: ", end="")
            languages = input().strip()
            new_assistant = TranslationAssistant(id, name, version, is_online, score, languages)
        elif type_choice == 5:
            print("请输入支持的语音语言（如中文,英文）: ", end="")
            voice_langs = input().strip()
            new_assistant = VoiceAssistant(id, name, version, is_online, score, voice_langs)
        
        if new_assistant:
            print(self.ai_manager.add_assistant(new_assistant))
    
    def get_valid_type_choice(self):
        while True:
            try:
                choice = int(input().strip())
                if 1 <= choice <= 5:
                    return choice
                else:
                    print("输入无效，请选择1-5之间的类型：", end="")
            except ValueError:
                print("输入无效，请输入数字（1-5）：", end="")
    
    def update_assistant_info(self):
        print("修改AI助手数据")
        print("请输入ID: ", end="")
        id = input().strip()
        print("可修改属性列表为：name,type,version,online,score")
        print("请输入要修改的属性和值（用空格分割）: ", end="")
        input_line = input().strip()
        if not input_line:
            print("❌ 输入不能为空！")
            return
        
        parts = input_line.split(" ", 1)
        if len(parts) < 2:
            print("❌ 输入格式错误，需为\"属性 值\"（如：score 100）")
            return
        
        property = parts[0]
        value = parts[1]
        print(self.ai_manager.update_assistant(id, property, value))
    
    def delete_assistant_by_id(self):
        print("请输入要删除的AI助手ID: ", end="")
        id = input().strip()
        print(self.ai_manager.delete_assistant(id))
    
    def start_assistant_service(self):
        print("请输入要启动的AI助手ID: ", end="")
        id = input().strip()
        print(self.ai_manager.start_assistant(id))
    
    def stop_assistant_service(self):
        print("请输入要停止的AI助手ID: ", end="")
        id = input().strip()
        print(self.ai_manager.stop_assistant(id))
    
    def assign_task_to_assistant(self):
        print("请输入目标AI助手ID: ", end="")
        id = input().strip()
        print("请输入任务内容: ", end="")
        task = input().strip()
        print(self.ai_manager.assign_task(id, task))
    
    def show_statistics(self):
        print(self.ai_manager.get_statistics())

# 程序入口
if __name__ == "__main__":
    app = Main()
    app.run()